﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 代码维护
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class CodeController : AdminBaseController
    {
        //
        // GET: /Admin/Code/
        /// <summary>
        /// 代码维护主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_SysManager", Order = 1)]
        [LogPageActionFilter("代码维护", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 编码树
        /// </summary>
        /// <returns>编码树视图</returns>
        public ActionResult CodeTree() {
            TreeModelList treeModelList = new TreeModelList();
            BLL.SysBasicCodeCls codeClsBll = new BLL.SysBasicCodeCls();
            List<Model.SysBasicCodeCls> codeClsAllList = codeClsBll.GetModelList(string.Empty);

            List<Model.SysBasicCodeCls> codeClsList = codeClsAllList.Where(c => c.FParentCodeClsNo == null).ToList();
            
            if(codeClsList.Count>0)
            {
                foreach(Model.SysBasicCodeCls codeCls in codeClsList){
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = codeCls.FCodeClsNo,
                        Name = codeCls.FCodeClsName,
                        ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

                    };

                    GetChildCode(treeParentItemModel, codeClsAllList);

                    treeParentItemModel.Id = "%!^" + treeParentItemModel.Id;
                    treeModelList.Add(treeParentItemModel);
                }
            }

            return View(treeModelList);
        }



        /// <summary>
        /// 递归编码树
        /// </summary>
        /// <param name="treeParentItemModel">树子节点</param>
        /// <param name="codeClsAllList">编码类别编号集合</param>
        public void GetChildCode(TreeItemModel treeParentItemModel, List<Model.SysBasicCodeCls> codeClsAllList) 
        {
            List<Model.SysBasicCodeCls> codeClsList = codeClsAllList.Where(c => c.FParentCodeClsNo == treeParentItemModel.Id).ToList();

            if (codeClsList.Count > 0) 
            {
                foreach (Model.SysBasicCodeCls codeCls in codeClsList) 
                {
                    TreeItemModel treeItemModel = new TreeItemModel(treeParentItemModel)
                    {
                        Id = codeCls.FCodeClsNo,
                        Name = codeCls.FCodeClsName,
                        ImageUrl = Url.Content("~/Content/ControlImages/page_code.png")
                    };
                    GetChildCode(treeItemModel, codeClsAllList);
                }                       
            }           
        }

        /// <summary>
        ///  编码列表页
        /// </summary>
        /// <param name="codeClsNo">编码类别编号</param>
        /// <returns>视图</returns>
        public ActionResult CodeList(string codeClsNo)
        {
            List<Model.SysBasicCodeName> codeNameList = new List<Model.SysBasicCodeName>();

            if (null != codeClsNo)
            {
                ViewData["blcode"] = true;
                ViewData["clsTitle"] = "编码类别： " + GetClsName(codeClsNo);
                codeNameList = GetCodeNameList(codeClsNo);
            }
            else
            {
                ViewData["blcode"] = false;
                ViewData["clsTitle"] = "请选择具体编码类别";
            }


            ViewData["codeClsNo"] = codeClsNo;

            return View(codeNameList);
        }


        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult CodeListPartial()
        {
            List<Model.SysBasicCodeName> codeNameList = new List<Model.SysBasicCodeName>();
            string codeClsNo = Request.Params["codeClsNo"] == null ? string.Empty : Request.Params["codeClsNo"];
            if (codeClsNo != "")
            {
                codeNameList = GetCodeNameList(codeClsNo);
            }
            ViewData["blcode"] = true;
            return PartialView("CodeListPartial", codeNameList);
        }

        /// <summary>
        /// 获取编码数据列表
        /// </summary>
        /// <param name="codeClsNo">编码类别编号</param>
        /// <returns>数据集合</returns>
        private List<Model.SysBasicCodeName> GetCodeNameList(string codeClsNo)
        {
            codeClsNo = Fetch.estimateString(codeClsNo, CheckGetEnum.Safety);
            BLL.SysBasicCodeName codeNameBll = new BLL.SysBasicCodeName();
            return codeNameBll.GetListByClsNo(codeClsNo, GlobalConstant.ENTITYNO);
        }

        /// <summary>
        /// 获取编码类型名称
        /// </summary>
        /// <param name="clsNo">类别编号</param>
        /// <returns>返回类型名称</returns>
        private string GetClsName(string clsNo)
        {
            clsNo = Fetch.estimateString(clsNo, CheckGetEnum.Safety);
            BLL.SysBasicCodeCls codeClsBll = new BLL.SysBasicCodeCls();
            return codeClsBll.GetCodeClsName(clsNo);
        }


        /// <summary>
        /// 编码增加修改
        /// </summary>
        /// <param name="codeClsNo">编码类别编码 必须有具体值</param>
        /// <param name="codeNo">编码</param>
        /// <returns>视图</returns>
        public ActionResult CodeEdit(string codeClsNo, string codeNo)
        {
            Model.SysBasicCodeName codeNameObj = new Model.SysBasicCodeName();
            ViewData["blcodeNo"] = true;
            codeClsNo = Fetch.estimateString(codeClsNo, CheckGetEnum.Safety);
            if (null != codeClsNo && null != codeNo)
            {
                codeNo = Fetch.estimateString(codeNo, CheckGetEnum.Safety);
                BLL.SysBasicCodeName codeNameBll = new BLL.SysBasicCodeName();
                codeNameObj = codeNameBll.GetModel(codeClsNo, GlobalConstant.ENTITYNO, codeNo);
                if (null == codeNameObj)
                {
                    codeNameObj = new Model.SysBasicCodeName();
                }
                else
                {
                    ViewData["blcodeNo"] = false;
                }
            }
            else
            {
                codeNameObj.FCodeClsNo = codeClsNo;
            }

            return View(codeNameObj);
        }


        /// <summary>
        /// 编码保存
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeName">编码名字</param>
        /// <param name="codeClsNo">编码类别编码</param>
        /// <param name="isAdd">是否添加true添加，false修改</param>
        /// <returns>是否添加成功</returns>
        public string CodeSave(string codeNo, string codeName, string codeClsNo, bool isAdd)
        {
            string str = string.Empty;

            codeNo = Fetch.estimateString(codeNo, CheckGetEnum.Safety);
            codeName = Fetch.estimateString(codeName, CheckGetEnum.Safety);
            codeClsNo = Fetch.estimateString(codeClsNo, CheckGetEnum.Safety);

            BLL.SysBasicCodeName codeNameBll = new BLL.SysBasicCodeName();
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(codeClsNo, GlobalConstant.ENTITYNO, codeNo))
                {
                    return "exists";
                }
            }

            Model.SysBasicCodeName codeNameObj = new Model.SysBasicCodeName();
            codeNameObj.FCodeClsNo = codeClsNo;
            codeNameObj.FCodeName = codeName;
            codeNameObj.FCodeNo = codeNo;
            codeNameObj.FDataType = "U";
            codeNameObj.FEntityNo = GlobalConstant.ENTITYNO;

            try
            {
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj);
                }
                else
                {
                    codeNameBll.Update(codeNameObj);
                }
                str = "ok";

            }
            catch (Exception)
            {

                str = "error";
            }

            return str;
        }


        /// <summary>
        /// 删除编码数据
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeClsNO">编码类别编码</param>
        /// <returns>是否删除成功</returns>
        public string DelCode(string codeNo, string codeClsNO)
        {
            string str = string.Empty;
            if (null != codeNo && null != codeClsNO)
            {
                codeNo = Fetch.estimateString(codeNo, CheckGetEnum.Safety);
                codeClsNO = Fetch.estimateString(codeClsNO, CheckGetEnum.Safety);

                BLL.SysBasicCodeName codeNameBll = new BLL.SysBasicCodeName();
                try
                {
                    codeNameBll.Delete(codeClsNO, GlobalConstant.ENTITYNO, codeNo);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }



    }
}
